﻿using System;
using Foothill.Common;
using Foothill.IoC;
using Foothill.Model;
using Foothill.Service.Configuration;
using Foothill.Service.UserAccount;

namespace Foothill.Web.AccountActivation
{
    public partial class AccountActivation : System.Web.UI.Page
    {
        private IAccountRegistrationManager _accountRegistrationManager;
        private IHostSettingManager _hostSettingManager;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                object token = Request.QueryString[GlobalQueryString.AuthCodeQsName];
                _accountRegistrationManager = IocContainer.GetInstance<IAccountRegistrationManager>();
                _hostSettingManager = IocContainer.GetInstance<IHostSettingManager>();

                int useAccountId = 0;
                string messageToUser;
                AccountActivationResult activationResult = _accountRegistrationManager.VerifyActivationCode(token, out useAccountId, out messageToUser);

                ProcessActivationResult(activationResult, useAccountId, messageToUser);            
            }
        }

        internal void ProcessActivationResult(AccountActivationResult activationResult, int useAccountId, string messageToUser)
        {
            if (activationResult == AccountActivationResult.Succeed)
            {
                // activation code OK, activate the account in database
                _accountRegistrationManager.ActivateNewAccount(useAccountId);

                Uri loginUri = new Uri(_hostSettingManager.GetSiteBaseUri(), "Login/Login.aspx");

                lblFeedback.Text = messageToUser;
                lblFeedback.CssClass = "activation-ok";

                string jsFunc = string.Format("RedirectToLoginPage(\"{0}\");", loginUri.ToString());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Redirect", jsFunc, true);
            }
            else if (activationResult == AccountActivationResult.InvalidToken)
            {
                // fail to verify activation code
                lblFeedback.Text = messageToUser;
                lblFeedback.CssClass = "activation-error";
            }
            else if (activationResult == AccountActivationResult.AlreadyActivated)
            {
                Uri loginUri = new Uri(_hostSettingManager.GetSiteBaseUri(), "Login/Login.aspx");

                lblFeedback.Text = messageToUser;
                lblFeedback.CssClass = "activation-error";

                string jsFunc = string.Format("RedirectToLoginPage(\"{0}\");", loginUri.ToString());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Redirect", jsFunc, true);
            }
            else if (activationResult == AccountActivationResult.Expired)
            {
                lblFeedback.Text = messageToUser;
                lblFeedback.CssClass = "activation-error";

                // todo: send another activation code
            }
        }
    }
}